package com.itsci.assignment2;

import java.util.ArrayList;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class RegisterManager extends SQLiteOpenHelper {

	public RegisterManager(Context context, String name, CursorFactory factory,
			int version) {
		super(context, "REGISTER", null, version);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL("CREATE TABLE REGISTER(userName text PRIMARY KEY,NAME TEXT,password text,email text);");
		
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
		db.execSQL("DROP TABLE Register");
		onCreate(db);
	}

	public void insertRegister(RegisterBean regisBean) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues content = new ContentValues();
		content.put("NAME", regisBean.getName());
		content.put("userName", regisBean.getUser());
		content.put("password",regisBean.getPass());
		content.put("email",regisBean.getEmail());
		db.insert("Register", null, content);
		db.close();
		
		
		
	}

	public void updateRegisterData(RegisterBean regisBean) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues content = new ContentValues();
		content.put("NAME", regisBean.getName());
		content.put("NAME", regisBean.getName());
		content.put("userName", regisBean.getUser());
		content.put("password",regisBean.getPass());
		content.put("email",regisBean.getEmail());
		db.update("Register", content, "userName = ?", new String[] { regisBean.getUser() });
		db.close();
	}

	public void deleteRegisterData(String user) {
		SQLiteDatabase db = this.getWritableDatabase();
		db.delete("Register", "userName = ?", new String[] { user });
		db.close();
	}
	
	public RegisterBean getByStudentId(String id) {
		SQLiteDatabase db = this.getWritableDatabase();
		Cursor cursor = db.query("Register", new String[] { "username","NAME","password","email"},
				"username = ?", new String[] { id }, null, null, null);
		RegisterBean regis = new RegisterBean();
		if (cursor.moveToFirst()) {
			regis.setUser(cursor.getString(0));
			regis.setName(cursor.getString(1));
			regis.setPass(cursor.getString(2));
			regis.setEmail(cursor.getString(3));
		} else {
			regis.setUser("");
			regis.setName("");
			regis.setPass("");
			regis.setEmail("");
		}
		db.close();
		return regis;
	}
	public ArrayList<RegisterBean> getAllRegisterBean() {
		SQLiteDatabase db = this.getWritableDatabase();
		Cursor cursor = db.query("Register", new String[] {"username","NAME","password","email"}, null, null, null, null, null);
		ArrayList<RegisterBean> arrayRegis = new ArrayList<RegisterBean>();
		if (cursor.moveToFirst()) {
			do {
				RegisterBean regis = new RegisterBean();				
				regis.setUser(cursor.getString(0));
				regis.setName(cursor.getString(1));
				regis.setPass(cursor.getString(2));
				regis.setEmail(cursor.getString(3));
				arrayRegis.add(regis);
			}while(cursor.moveToNext());
		}
		db.close();
		return arrayRegis;
	}
}
